package com.zuxia.impDao;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.zuxia.iDao.DepartmentinfoDAOI;
import com.zuxia.po.Departmentinfo;
import com.zuxia.po.Staffindo;

/**
 * 部门实现dao类
 * @author 谌华勇
 * 2018年11月12日下午7:28:40
 */
public class DepartmentinfoDAO extends HibernateDaoSupport implements DepartmentinfoDAOI {
	private static final Logger log = LoggerFactory
			.getLogger(DepartmentinfoDAO.class);
	// property constants
	public static final String DP_INAME = "dpIname";
	public static final String DP_IDUTY = "dpIduty";

	protected void initDao() {
		// do nothing
	}

	public int save(Departmentinfo transientInstance) {
		log.debug("saving Departmentinfo instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			return 0;
		}
		return 1;
	}

	public int delete(Departmentinfo persistentInstance) {
		log.debug("deleting Departmentinfo instance");
		try {
			Departmentinfo dt= getHibernateTemplate().get(Departmentinfo.class,persistentInstance.getDpIid());		
			getHibernateTemplate().delete(dt);
			log.debug("delete successful");
			return 1;
		} catch (RuntimeException re) {
			re.printStackTrace();
			log.error("delete failed", re);
			return 0;
		}
	}
	public int selAllCout(String serachstuName){
		log.debug("sel ALL Staffindo For PageCount");
		StringBuffer hql = new StringBuffer("select count(*) from Departmentinfo as a ");
		if(!serachstuName.equals("")) {
			hql.append("where dpIname like "+"'%"+serachstuName+"%'");
		}
		long id=(Long) getHibernateTemplate().iterate(hql.toString()).next();
		return (int)id;
	}

	public Departmentinfo findById(java.lang.Integer id) {
		log.debug("getting Departmentinfo instance with id: " + id);
		try {
			Departmentinfo instance = (Departmentinfo) getHibernateTemplate()
					.get("com.zuxia.po.Departmentinfo", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/**
	 * 查询所有部门的id和名字
	 */
	public List findAllDepartmentFordpIidAnddpIname() {
		String queryString = "select new Departmentinfo(dpIid,dpIname) from Departmentinfo as a";
		try {
			return getHibernateTemplate().find(queryString);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public List findByExample(Departmentinfo instance) {
		log.debug("finding Departmentinfo instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.zuxia.impDao.DepartmentinfoDAOI#findByProperty(java.lang.String, java.lang.Object)
	 */
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Departmentinfo instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from Departmentinfo as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.zuxia.impDao.DepartmentinfoDAOI#findByDpIname(java.lang.Object)
	 */
	public List findByDpIname(Object dpIname) {
		return findByProperty(DP_INAME, dpIname);
	}

	/* (non-Javadoc)
	 * @see com.zuxia.impDao.DepartmentinfoDAOI#findByDpIduty(java.lang.Object)
	 */
	public List findByDpIduty(Object dpIduty) {
		return findByProperty(DP_IDUTY, dpIduty);
	}

	/* (non-Javadoc)
	 * @see com.zuxia.impDao.DepartmentinfoDAOI#findAll()
	 */
	public List findAll() {
		log.debug("finding all Departmentinfo instances");
		try {
			String queryString = "from Departmentinfo";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
	public List<Departmentinfo> selAllDeparByPageModel(final String pageNumber, final String limit, String serachstuName) {
		//final Object[] values
		log.debug("selAllDeparByPageModel"); 
		final StringBuffer hql = new StringBuffer("from Departmentinfo as a ");
		if(!serachstuName.equals("")) {
			hql.append("where dpIname like "+"'%"+serachstuName+"%'");
		}
		List list = getHibernateTemplate().executeFind(new HibernateCallback() {
			   
			   public Object doInHibernate(Session session) throws HibernateException,
			     SQLException {
			    Query query = session.createQuery(hql.toString());
			    //这个用于扩展多条件查询用到的
			   /* for (int i = 0; i < values.length; i++) {
			     query.setParameter(i, values[i]);
			    }*/
			    int p = Integer.parseInt(pageNumber);
			    int l = Integer.parseInt(limit);
			    int a = (p-1)*l;
			    List result = query.setFirstResult(a)
			        .setMaxResults(l)
			        .list();
			    /*List result = query
				        .list();*/
			    System.out.println("部门dao中查询到了数据"+result);
			    return result;
			   }
			  });
		
			  return list;
	}

	public int merge(Departmentinfo detachedInstance) {
		log.debug("merging Departmentinfo instance");
		try {
			Departmentinfo result = (Departmentinfo) getHibernateTemplate()
					.merge(detachedInstance);
			log.debug("merge successful");
			return result==null?0:1;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			return 0;
		}
	}

	public void attachDirty(Departmentinfo instance) {
		log.debug("attaching dirty Departmentinfo instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Departmentinfo instance) {
		log.debug("attaching clean Departmentinfo instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static DepartmentinfoDAOI getFromApplicationContext(
			ApplicationContext ctx) {
		return (DepartmentinfoDAOI) ctx.getBean("DepartmentinfoDAO");
	}
	
	
}